//
//  SGAppDelegate.m
//  EatNumber
//
//  Created by zg.shao on 14/12/19.
//  Copyright (c) 2014年 kuwo.cn. All rights reserved.
//

#import "SGAppDelegate.h"
#import "SGTools.h"
#import "MobClick.h"
#import "WXApi.h"
#import "QQApi.h"
#import "TencentOAuth.h"
#import "Appirater.h"

static SystemSoundID s_game_default_sound_id = 0;

@interface SGAppDelegate ()

@end

@implementation SGAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    _window = [[SGWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _window.backgroundColor = [UIColor whiteColor];
    
    _rootViewController = [[SGLevelViewController alloc] init];
    _rootNavController = [[SGBaseNavigationController alloc] initWithRootViewController:_rootViewController];
    _rootNavController.navigationBarHidden = YES;
    
    _window.rootViewController = _rootNavController;
    
    [SGAppDelegate registerGameSound];
    [SGAppDelegate registerThirdPartyServiceWithOptions:launchOptions];
    
    [_window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    // 更新在线参数
    [MobClick updateOnlineConfig];
    
    [Appirater appEnteredForeground:YES];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    NSString *schema = [url scheme];
    if ([schema isEqualToString:WeiXinAppSecret]) {
        return [WXApi handleOpenURL:url delegate:_wxApiDelegate];
    } else if ([schema isEqualToString:QQApiSchema]) {
        if (_qqApiInterfaceDelegate) {
            [QQApiInterface handleOpenURL:url delegate:_qqApiInterfaceDelegate];
        }
        return [TencentOAuth HandleOpenURL:url];
    }
    
    return NO;
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
//    NSString *urlString = [url absoluteString];
//    if ([urlString hasPrefix:@""]) {
//        return [WeiboSDK handleOpenURL:url delegate:_weiboDelegate];
//    } else {
//        return [TencentOAuth HandleOpenURL:url];
//    }
    return NO;
}

+ (void)registerGameSound {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"default_sound" ofType:@"wav"];
    if (path) {
        //注册声音到系统
        AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &s_game_default_sound_id);
//        AudioServicesPlaySystemSound(s_game_default_sound_id);
    }
}

+ (void)playGameDefaultSoundWithState:(BOOL)state {
    if (state) {
        AudioServicesPlaySystemSound(s_game_default_sound_id);
    }
}

+ (void)registerThirdPartyServiceWithOptions:(NSDictionary *)launchOptions {
    // 友盟
    [MobClick startWithAppkey:UMENG_APP_KEY];
    [MobClick setAppVersion:[SGConstant appVersionString]];
    [MobClick setCrashReportEnabled:YES];
    [MobClick setLogEnabled:NO];
    
    // 更新在线参数
    [MobClick updateOnlineConfig];
    
    // 微信
    [WXApi registerApp:WeiXinAppID];
    
    // 评价框相关
    [Appirater setAppId:APP_ID];
    [Appirater setDaysUntilPrompt:1];
    [Appirater setUsesUntilPrompt:3];
    [Appirater setSignificantEventsUntilPrompt:-1];
    [Appirater setTimeBeforeReminding:2];
#ifdef DEBUG
//    [Appirater setDebug:YES];
#endif
    [Appirater appLaunched:YES];
}

+ (SGAppDelegate *)appDelegate {
    SGAppDelegate *delegate = (SGAppDelegate *)[UIApplication sharedApplication].delegate;
    return delegate;
}

@end
